package com.generate.tools.generate;

import java.util.Map;

import com.generate.tools.utils.StringUtils;

public abstract class AbstractGenerateEntity {
	
	protected boolean f_util = false; // 是否需要导入包java.util.*
	protected boolean f_sql = false; // 是否需要导入包java.sql.*
	private String prefix;
	private String[] columns; // 列名数组
	private String[] colTypes; // 列名类型数组
	private int[] columnSizes; // 列名大小数组
	private String tableName;
	private String packageName;
	private String mapperPackageName;
	private String className;
	
	public String getEntityMapper() {
		StringBuilder mapper = new StringBuilder();
		mapper.append("package ");
		mapper.append(mapperPackageName+";\r\n");
		mapper.append("import com.fast.mybatis.mapper.MybatisMapper;\r\n");
		mapper.append("import org.springframework.stereotype.Repository;\r\n");
		mapper.append("@Repository \r\n");
		mapper.append("public interface "+className+"Mapper extends MybatisMapper<"+className+">{\r\n");
		
		mapper.append("}");
		return String.valueOf(mapper);
	}
	
	public String getClassName(final String tableName,String prefix){
		StringBuilder sb = new StringBuilder(tableName);
		if(tableName.toUpperCase().startsWith(prefix.toUpperCase())){
			sb.delete(0, prefix.length());
		}
		return StringUtils.getColumnName(String.valueOf(sb));
	}
	
	/**
	 * 功能：生成实体类主体代码
	 * 
	 * @param columns
	 * @param colTypes
	 * @param colSizes
	 * @return
	 */
	public String parse(Map<String, String> commentMap,Map<String, String> tableCommentMap) {
		StringBuilder sb = new StringBuilder();

		sb.append("package " + packageName + ";\r\n");

		// 判断是否导入工具包
		if (f_util) {
			sb.append("import java.util.Date;\r\n");
		}
		if (f_sql) {
			sb.append("import java.sql.*;\r\n");
		}
		sb.append("import com.fast.mybatis.annotation.*;\r\n");
		sb.append("import com.fast.mybatis.constants.JavaType;\r\n");
		sb.append("import org.apache.ibatis.type.JdbcType;\r\n");
		sb.append("\r\n");
		// 注释部分
		if(tableCommentMap.get(tableName) != null) {
			sb.append("   /**\r\n");
			sb.append("    * " +tableCommentMap.get(tableName)+ "\r\n");
			sb.append("    */ \r\n");
		}
		//sb.append("@Setter\r\n");
		//sb.append("@Getter\r\n");
		sb.append("@Table(name = \"" + tableName + "\" )");
		sb.append("\npublic class " + StringUtils.upperCase(getClassName(tableName,prefix)) + "{\r\n");
		sb.append(processAllAttrs(commentMap));
		sb.append(processAllMethod());
		sb.append("}\r\n");
		return sb.toString();
	}

	public String[] getColumns() {
		return columns;
	}

	public void setColumns(String[] columns) {
		this.columns = columns;
	}

	public String[] getColTypes() {
		return colTypes;
	}

	public void setColTypes(String[] colTypes) {
		this.colTypes = colTypes;
	}


	public int[] getColumnSizes() {
		return columnSizes;
	}

	public void setColumnSizes(int[] columnSizes) {
		this.columnSizes = columnSizes;
	}

	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public String getPackageName() {
		return packageName;
	}

	public void setPackageName(String packageName) {
		this.packageName = packageName;
	}

	/**
	 * 功能：生成所有属性
	 * 
	 * @param sb
	 */
	public String processAllAttrs( Map<String, String> commentMap) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < columns.length; i++) {
			if(commentMap.get(columns[i]) != null) {
				sb.append("\t/**").append(commentMap.get(columns[i])).append("**/\r\n");
			}
			if(i == 0) {
				sb.append("\t@Id\r\n");
			}
			sb.append("\t@Column(columnName=\""
						+ columns[i] + "\"");
			sb.append(",javaType=JavaType." + sqlType2JavaType(colTypes[i]).toUpperCase());
			sb.append(",jdbcType=JdbcType." + sqlType2JdbcType(colTypes[i]) + ")\r\n");
			sb.append("\tprivate " + sqlType2JavaType(colTypes[i]) + " "
					+ StringUtils.getColumnName(StringUtils.lowerCase(columns[i])) + ";\r\n");
		}
		return String.valueOf(sb);
	}

	/**
	 * 功能：生成所有方法
	 * 
	 * @param sb
	 */
	public  String processAllMethod() {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < columns.length; i++) {
			sb.append("\tpublic void set" + StringUtils.getColumnName(StringUtils.upperCase(columns[i])) + "("
					+ sqlType2JavaType(colTypes[i]) + " "
					+ StringUtils.getColumnName(StringUtils.lowerCase(columns[i])) + "){\r\n");
			sb.append("\t  this." + StringUtils.getColumnName(StringUtils.lowerCase(columns[i])) + "="
					+ StringUtils.getColumnName(StringUtils.lowerCase(columns[i]) + ";\r\n"));
			sb.append("\t}\r\n");
			sb.append("\tpublic " + sqlType2JavaType(colTypes[i]) + " get"
					+ StringUtils.getColumnName(StringUtils.upperCase(columns[i]) + "(){\r\n"));
			sb.append("\t  return " + StringUtils.getColumnName(StringUtils.lowerCase(columns[i]) + ";\r\n"));
			sb.append("\t}\r\n");
		}
		return String.valueOf(sb);

	}

	
	
	public abstract String sqlType2JavaType(String sqlType);
	
	public abstract String sqlType2JdbcType(String sqlType);

	public String getPrefix() {
		return prefix;
	}

	public void setPrefix(String prefix) {
		this.prefix = prefix;
	}

	public String getClassName() {
		return className;
	}

	public void setClassName(String className) {
		this.className = className;
	}

	public String getMapperPackageName() {
		return mapperPackageName;
	}

	public void setMapperPackageName(String mapperPackageName) {
		this.mapperPackageName = mapperPackageName;
	}
	
	
}
